﻿
@using Gx.Model.Entity;
@using Gx.Model.Enums;
@{
    ViewBag.Title = "新建商品入库单";
    Layout = "~/Views/Shared/InfoMaste.cshtml";
}
<link href="~/Content/css/custome.css" rel="stylesheet" />
<div title="新建商品入库单" style="width:auto; height:auto; padding-left:5px; font-size:14px;  ">
    <div class="ftitle">
        <a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok" style="height:32px" onclick="saveData(false)">保存</a>
        <a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok" style="height:32px" onclick="confirmLKWarehouseEntry()">确认入库</a>
        <a href="/GoodsOrder/LKWarehouseEntryList" class="easyui-linkbutton" iconCls="icon-back" style="height:32px">返回</a>
    </div>
    <form id="fm" method="post" novalidate>
        <div class="ftitle">基础信息</div>
        <div class="fitem1">
            <label>
                单据编号:
            </label>
            <input class="easyui-textbox" readonly="readonly" value="系统自动生成" style="width: 173px; height: 30px;">
            <label>
                入库时间:
            </label>
            <input class="easyui-datebox" name="EntryTime" id="EntryTime" value="@Model.EntryTime" style="width: 173px; height: 30px; " data-options="required:true">
            <label>
                车牌号:
            </label>
            <input name="CarNumber" id="CarNumber" value="@Model.CarNumber" class="easyui-textbox" style="width: 173px; height: 30px;" />
            <label>
                搬运工单号:
            </label>
            <input name="PorterNo" id="PorterNo" value="@Model.PorterNo" class="easyui-textbox" style="width: 173px; height: 30px;" />
        </div>
        <div class="fitem1">
            <label>
                客户:
            </label>
            <input type="hidden" id="hidCustomerId" />
            <select class="easyui-combogrid" style="width: 173px; height: 30px;" name="CustomerId" id="CustomerId"></select>
            <label>
                客户名称:
            </label>
            <input name="CustomerName" id="CustomerName" class="easyui-textbox" style="width: 173px; height: 30px;" readonly="readonly" />
            <label>
                合同编号:
            </label>
            <input type="hidden" id="ContractId" name="ContractId" />
            <select class="easyui-combogrid" value="@Model.ContractNo" style="width: 173px; height: 30px;" name="ContractNo" id="ContractNo"></select>
            <label>
                租仓类型:
            </label>
            <input type="hidden" id="RentalWarehouseType" name="RentalWarehouseType" />
            <input name="RentalWarehouseTypeName" id="RentalWarehouseTypeName" class="easyui-textbox" style="width: 173px; height: 30px;" readonly="readonly" />
        </div>
        <div class="fitem1">
            <label>
                计费单位:
            </label>
            <input type="hidden" name="ChargingUnit" id="ChargingUnit" />
            <input name="ChargingUnitName" id="ChargingUnitName" class="easyui-textbox" style="width: 173px; height: 30px;" readonly="readonly" />
            <label>
                冷藏算法:
            </label>
            <input type="hidden" name="ColdAlgorithm" id="ColdAlgorithm" />
            <input name="ColdAlgorithmName" id="ColdAlgorithmName" class="easyui-textbox" style="width: 173px; height: 30px;" readonly="readonly" />
            <label>
                周期:
            </label>
            <input name="Cycle" id="Cycle" value="@Model.Cycle" class="easyui-numberbox" data-options="required: true" style="width: 173px; height: 30px;" autocomplete="off" />
            <label>
                冷藏单价:
            </label>
            <input name="ColdUnitPrice" id="ColdUnitPrice" value="@Model.ColdUnitPrice" class="easyui-numberbox" data-options="required: true,precision:'2'" style="width: 173px; height: 30px;" autocomplete="off" />
        </div>
        <div class="fitem1">
            <label>
                总吨重:
            </label>
            <input name="TotalTon" id="TotalTon" value="@Model.TotalTon" class="easyui-numberbox" data-options="required: true,precision:'4'" style="width: 173px; height: 30px;" autocomplete="off" />
            <label>
                总件数:
            </label>
            <input name="Total" id="Total" value="@Model.Total" class="easyui-numberbox" data-options="required: true" style="width: 173px; height: 30px;" autocomplete="off" />
            <label>
                总金额:
            </label>
            <input name="Amount" id="Amount" value="@Model.Amount" class="easyui-numberbox" data-options="required: true,precision:'2'" style="width: 173px; height: 30px;" autocomplete="off" />
            <label>
                不足n吨算n吨:
            </label>
            <input name="Tonnage" id="Tonnage" value="@Model.Tonnage" class="easyui-numberbox" data-options="required: true" style="width: 173px; height: 30px;" autocomplete="off" />
        </div>
        <div class="fitem1">
            <label>
                柜号:
            </label>
            <input name="ContainerNumber" id="ContainerNumber" value="@Model.ContainerNumber" class="easyui-textbox" style="width: 173px; height: 30px;" />
            <label>
                按吨操作:
            </label>
            <input class="easyui-switchbutton" id="sbIsByTon" data-options="onText:'是',offText:'否'">
        </div>
        <div class="fitem1">
            <label>
                图片:
            </label>
            <input type="hidden" id="hidFileId" />
            <input id="filebox" class="easyui-filebox" name="file" data-options="buttonText:'选择文件',accept:'image/*',onChange:fileChange" />
        </div>
        <div class="fitem1">
            <label>
                备注:
            </label>
            <textarea rows="4" cols="4" style="width: 400px; height: 90px; vertical-align: top" name="Remark" id="Remark" autocomplete="off" placeholder="200字以内"></textarea>
        </div>

        <div id="Wl">
            <div id="tabs" class="easyui-tabs" fit="true" border="false" style="width: 500px; height: 300px; padding-top: 5px;">
                <div title="商品明细">
                    <a href="javascript:" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addDom()">添加</a>
                    <table class="gridtable" id="detailgird">
                        <thead>
                            <tr>
                                <th>商品批号</th>
                                <th>商品</th>
                                <th>规格</th>
                                <th>仓位</th>
                                <th>单件重量</th>
                                <th>单件数量</th>
                                <th>面积</th>
                                <th>每板件数</th>
                                <th>总吨重</th>
                                <th>板数</th>
                                <th>生产日期</th>
                                <th>保质日期</th>
                                <th>产地</th>
                                <th>备注</th>
                                <th>已处理</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                    </table>
                </div>
                <div title="其他费用">
                    <a href="javascript:" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addDom2()">添加</a>
                    <table class="gridtable" id="costgird">
                        <thead>
                            <tr>
                                <th>费用类型</th>
                                <th>杂费单价</th>
                                <th>单位</th>
                                <th>计算数量</th>
                                <th>金额</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
        @Html.Partial("~/Views/Shared/_CreateUserAndModifyUser.cshtml")
    </form>
    <div id="customerselecttoolbar2">
        <input id="SelectCustomerText2" class="easyui-textbox " style="width: 173px; height: 30px;" />
        <a href="javascript:" class="easyui-linkbutton" plain="true" iconcls="icon-search" onclick="querycustomer2()">查询</a>
    </div>
</div>
<script src="~/Js/Global.js?v=20240706"></script>
<script type="text/javascript">

    $(function () {
        InitCustomer(initContract);
    });


    function initContract(row) {
        /*$("#hidCustomerId").val(row.Id);*/
        $('#ContractNo').combogrid({
            url: "/LKWarehouseContract/GetContractByCustomerId?customerId=" + row.Id,
            method: 'post',
            mode: 'remote',
            panelWidth: 500,
            idField: 'ContractNo',
            textField: 'ContractNo',
            onSelect: function () {
                var g = $('#ContractNo').combogrid('grid');
                var row = g.datagrid('getSelected');
                //$('#RentalWarehouseTypeName').textbox('setValue', row.ContractTypeName);
                //$('#RentalWarehouseType').val(row.ContractType)
                //if (row.ContractType == 1) {
                //    $('#ChargingUnitName').textbox('setValue', row.ChargingUnitName);
                //    $('#ChargingUnit').val(row.ChargingUnit);

                //    $('#ColdAlgorithmName').textbox('setValue', row.ColdAlgorithmName);
                //    $('#ColdAlgorithm').val(row.ColdAlgorithm);

                //    $('#Cycle').numberbox('setValue', row.Cycle);
                //    $('#ColdUnitPrice').numberbox('setValue', row.ColdUnitPrice);
                //}
                loadContractInfo(row);
            },
            columns: [[
                { field: 'ContractType', title: 'ContractType', hidden: true },
                { field: 'ContractNo', title: '合同', width: 120, align: 'right' },
                { field: 'ContractTypeName', title: '合同类型', width: 150, align: 'right' },
                { field: 'ChargingUnit', title: 'ChargingUnit', hidden: true },
                { field: 'ChargingUnitName', title: 'ChargingUnitName', hidden: true },
                { field: 'ColdAlgorithm', title: 'ColdAlgorithm', hidden: true },
                { field: 'ColdAlgorithmName', title: 'ColdAlgorithmName', hidden: true },
                { field: 'Cycle', title: 'Cycle', hidden: true },
                { field: 'ColdUnitPrice', title: 'ColdUnitPrice', hidden: true },
            ]],
            onLoadSuccess: function (data) {
                //var dataCount = data.total; // 获取加载成功后的数据条数
                if (data.total == 1) {
                    var row = data.rows[0];
                    loadContractInfo(row);
                }
            }
        });

    }

    function loadContractInfo(row) {
        $('#ContractNo').combogrid('setValue', row.ContractNo);
        $('#ContractId').val(row.ContractId);
        $('#RentalWarehouseTypeName').textbox('setValue', row.ContractTypeName);
        $('#RentalWarehouseType').val(row.ContractType)

        $('#ChargingUnitName').textbox('enable');
        $('#ColdAlgorithmName').textbox('enable');
        $('#Cycle').numberbox('enable');
        $('#ColdUnitPrice').numberbox('enable');

        $('#ChargingUnitName').textbox('setValue', row.ChargingUnitName);
        $('#ChargingUnit').val(row.ChargingUnit);

        $('#ColdAlgorithmName').textbox('setValue', row.ColdAlgorithmName);
        $('#ColdAlgorithm').val(row.ColdAlgorithm);

        $('#Cycle').numberbox('setValue', row.Cycle);
        $('#ColdUnitPrice').numberbox('setValue', row.ColdUnitPrice);

        if (row.ContractType != 1) {
            $('#ChargingUnitName').textbox('disable');
            $('#ColdAlgorithmName').textbox('disable');
            $('#Cycle').numberbox('disable');
            $('#ColdUnitPrice').numberbox('disable');
        }
    }

    var domId = 1;
    function addDom() {
        $.post("/GoodsOrder/_AddLKWarehouseEntryDetail", { domId: domId }, function (html) {
            $.messager.progress('close');
            $("#detailgird").append(html);
            $.parser.parse($("#" + domId));
            InitWareHouse(domId);
            InitGoods(domId);
            InitGoodsSpecCombobox(domId);
            domId = Number(domId) + 1;

            initDom();
        });
    }

    function delDom(id) {
        $("#" + id).remove();
        //domId = Number(domId) - 1;
        removecalculateWeight();
    }

    var domId2 = 1;
    function addDom2() {
        $.post("/GoodsOrder/_AddLKWarehouseEntryCost", { domId: domId2 }, function (html) {
            $.messager.progress('close');
            $("#costgird").append(html);
            $.parser.parse($("#CostTr" + domId2));
            InitCostTypesCombogrid2(domId2, initCostType);
            InitChargingUnitCombobox(domId2);
            domId2 = Number(domId2) + 1;

            initDom();
        });
    }

    function delDom2(id) {
        $("#CostTr" + id).remove();

        //domId2 = Number(domId2) - 1;
        removecalculateAmount();
    }

    function initCostType(row) {
        var id = row.did;
        $("#CostTypeId" + id).val(row.data.Id);//combobox('setValue', rowData.Id);
        $("#CostType" + id).combobox('setValue', row.data.CostTypeName);
        $("#ChargingUnit" + id).combobox('setValue', row.data.Unit);
        $("#UnitPrice" + id).numberbox('setValue', row.data.UnitPrice);
    }

    function querycustomer2() {
        $("#CustomerId").combogrid("grid").datagrid("reload", { "Customer": $("#SelectCustomerText2").val() });
    }

    var costdom = new Array();
    var detaildom = new Array();

    function initDom() {
        costdom.length = 0;
        detaildom.length = 0;
        $("input[type='hidden'][id^='CostDomId']").each(function () {
            var d = $(this).val();
            if ($.inArray(d, costdom) === -1) {
                costdom.push(d);
            }
        });
        $("input[type='hidden'][id^='DetailDomId']").each(function () {
            var d = $(this).val();
            if ($.inArray(d, detaildom) === -1) {
                detaildom.push(d);
            }
        });

    }

    //计算总重量
    function calculateWeight() {
        initDom();
        var triggerControl = this;
        var value = $(triggerControl).numberbox('getValue');

        //获取triggerControl的id的最后一个字符
        var id = $(triggerControl).attr("id");
        var lastChar = id.substr(id.length - 1, 1);
        var number = $("#Number" + lastChar).numberbox('getValue') * 1;
        var weight = $("#Weight" + lastChar).numberbox('getValue') * 1;
        var tonweight = (number * weight.toFixed(4)) / 1000;
        $("#TonWeight" + lastChar).numberbox('setValue', tonweight);

        var totalton = 0;
        var total = 0;
        detaildom.forEach(function (value, index) {
            var i = value;
            var numbers = $("#Number" + i).numberbox('getValue') * 1;
            var weights = $("#Weight" + i).numberbox('getValue') * 1;

            var tonweight = (numbers * weights.toFixed(4)) / 1000;
            totalton += tonweight;
            total += numbers;
        });
        $("#TotalTon").numberbox('setValue', totalton);
        $("#Total").numberbox('setValue', total);
    }

    function removecalculateWeight() {
        initDom();
        var totalton = 0;
        var total = 0;
        detaildom.forEach(function (value, index) {
            var i = value;
            var numbers = $("#Number" + i).numberbox('getValue') * 1;
            var weights = $("#Weight" + i).numberbox('getValue') * 1;

            var tonweight = (numbers * weights.toFixed(4)) / 1000;
            totalton += tonweight;
            total += numbers;
        });
        $("#TotalTon").numberbox('setValue', totalton);
        $("#Total").numberbox('setValue', total);
    }


    function calculateAmount() {
        initDom();
        var triggerControl = this;
        var value = $(triggerControl).numberbox('getValue');

        //获取triggerControl的id的最后一个字符
        var id = $(triggerControl).attr("id");
        var lastChar = id.substr(id.length - 1, 1);
        var number = $("#CNumber" + lastChar).numberbox('getValue') * 1;
        var unitprice = $("#UnitPrice" + lastChar).numberbox('getValue') * 1;
        var amount = (number * unitprice).toFixed(2);
        $("#Amount" + lastChar).numberbox('setValue', amount);

        var totalAmount = 0.00;
        costdom.forEach(function (value, index) {
            var i = value;
            var numbers = $("#CNumber" + i).numberbox('getValue') * 1;
            var unitprices = $("#UnitPrice" + i).numberbox('getValue') * 1;

            var tonam = (numbers * unitprices).toFixed(2) * 1;
            totalAmount += tonam;
        });
        $("#Amount").numberbox('setValue', totalAmount);
    }

    function removecalculateAmount() {
        initDom();
        var totalAmount = 0.00;
        costdom.forEach(function (value, index) {
            var i = value;
            var numbers = $("#CNumber" + i).numberbox('getValue') * 1;
            var unitprices = $("#UnitPrice" + i).numberbox('getValue') * 1;

            var tonam = (numbers * unitprices).toFixed(2) * 1;
            totalAmount += tonam;
        });
        $("#Amount").numberbox('setValue', totalAmount);
    }



    function saveData(isconfirm) {
        var validate = $("#fm").form("validate")
        if (!validate) {
            return;
        }
        var status = 0;
        if (isconfirm) {
            status = 1;
        }
        var data = {
            "EntryTime": $("#EntryTime").datebox('getValue'),
            "CarNumber": $("#CarNumber").textbox('getValue'),
            "PorterNo": $("#PorterNo").textbox('getValue'),
            "CustomerId": $("#CustomerId").combobox('getValue'),//$("#hidCustomerId").val(),
            "CustomerNo": $("#CustomerId").combobox('getText'),
            "CustomerName": $("#CustomerName").textbox('getValue'),
            "ContractNo": $("#ContractNo").textbox('getValue'),
            "RentalWarehouseType": $("#RentalWarehouseType").val(),//.combobox('getValue'),
            "ChargingUnit": $("#ChargingUnit").val(),//.combobox('getValue'),
            "ColdAlgorithm": $("#ColdAlgorithm").val(),//.combobox('getValue'),
            "Cycle": $("#Cycle").numberbox('getValue'),
            "ColdUnitPrice": $("#ColdUnitPrice").numberbox('getValue'),
            "TotalTon": $("#TotalTon").numberbox('getValue'),
            "Total": $("#Total").numberbox('getValue'),
            "Amount": $("#Amount").numberbox('getValue'),
            "Tonnage": $("#Tonnage").numberbox('getValue'),
            "IsByTon": $('#sbIsByTon').switchbutton('options').checked,
            "ContainerNumber": $("#ContainerNumber").textbox('getValue'),
            "Remark": $("#Remark").val(),
            "Status": status
        };



        var detailList = new Array();
        detaildom.forEach(function (value, index) {
            var i = value;
            var GoodsNo = $("#GoodsNo" + i).textbox('getValue');
            var GoodsId = $("#GoodsId" + i).combobox('getValue');
            var GoodsSpecId = $("#GoodsSpecId" + i).combobox('getValue');
            var WareHouseId = $("#WareHouseId" + i).combobox('getValue');

            var GoodsName = $("#GoodsId" + i).combobox('getText');
            var GoodsSpecName = $("#GoodsSpecId" + i).combobox('getText');
            var WareHouseName = $("#WareHouseId" + i).combobox('getText');

            var Weight = $("#Weight" + i).numberbox('getValue');
            var number = $("#Number" + i).numberbox('getValue');
            var Area = $("#Area" + i).numberbox('getValue');
            var AvNumber = $("#AvNumber" + i).numberbox('getValue');
            var TonWeight = $("#TonWeight" + i).numberbox('getValue');
            var PlateNumber = $("#PlateNumber" + i).numberbox('getValue');
            var ProductionDate = $("#ProductionDate" + i).datebox('getValue');
            var ExpirationDate = $("#ExpirationDate" + i).datebox('getValue');
            var ProducingArea = $("#ProducingArea" + i).textbox('getValue');
            var Remark = $("#Remark" + i).textbox('getValue');
            //var Remark1 = $("#Remark1" + i).textbox('getValue');
            //var Remark2 = $("#Remark2" + i).textbox('getValue');
            //var Remark3 = $("#Remark3" + i).textbox('getValue');
            var Processed = $("#sbProcessed" + i).switchbutton('options').checked;

            var detail = {
                "GoodsNo": GoodsNo,
                "GoodsId": GoodsId,
                "GoodsSpecId": GoodsSpecId,
                "WareHouseId": WareHouseId,
                "GoodsName": GoodsName,
                "GoodsSpecName": GoodsSpecName,
                "WareHouseName": WareHouseName,
                "Weight": Weight,
                "Number": number,
                "Area": Area,
                "AvNumber": AvNumber,
                "TonWeight": TonWeight,
                "PlateNumber": PlateNumber,
                "ProductionDate": ProductionDate,
                "ExpirationDate": ExpirationDate,
                "ProducingArea": ProducingArea,
                "Remark": Remark,
                //"Remark1": Remark1,
                //"Remark2": Remark2,
                //"Remark3": Remark3,
                "Processed": Processed,
            };
            detailList.push(detail);
        });

        var costList = new Array();
        costdom.forEach(function (value, index) {
            var i = value;
            var CostId = $("#CostType" + i).combobox('getValue');
            var UnitPrice = $("#UnitPrice" + i).numberbox('getValue');
            var Unit = $("#ChargingUnit" + i).numberbox('getValue');
            var UnitName = $("#ChargingUnit" + i).combobox('getText');
            var number = $("#CNumber" + i).numberbox('getValue');
            var Amount = $("#Amount" + i).numberbox('getValue');

            var cost = {
                "CostId": CostId,
                "UnitPrice": UnitPrice,
                "Unit": Unit,
                "UnitName": UnitName,
                "Number": number,
                "Amount": Amount,
            };
            costList.push(cost);
        });

        var d = JSON.stringify({ WarehouseEntry: data, Details: detailList, Costs: costList, AttachmentId: $("#hidFileId").val() });


        $.messager.progress({
            title: '请稍后',
            msg: '数据保存中...',
        });
        window.setTimeout(function () {
            $.ajax({
                url: '/GoodsOrder/AddLKWarehouseEntry',
                type: "post",
                data: d,
                contentType: 'application/json; charset=utf-8',
                success: function (json) {
                    $.messager.progress('close');
                    Helper.Show(json.Msg);
                    if (json.Success) {
                        setTimeout(function () { window.location.href = "/GoodsOrder/LKWarehouseEntryList"; }, 2000);
                    }
                }
            });
        }, 2000);
    }

    //确认入库
    function confirmLKWarehouseEntry() {
        if (confirm("确认入库后不可更改,确认入库吗?")) {
            saveData(true);
        }
    }
</script>





